# -*- coding: utf-8 -*-
# @Date：2024/10/5 00:04:40
# @Author：CJiaの青姝
# @FileName：statistic
# @Editor：PyCharm2024
# @Remark：
from application.models import AppLog
from ninja import Router, Field, Query, Schema

from utils.cj_ninja import CjFilters
from utils.cj_crud import retrieve
from utils.cj_response import SuccessResponse, ErrorResponse

router = Router()


class Filters(CjFilters):
    app_id: int = Field(None, description="应用详情id", alias="app_id")
    create_time: str = Field(None, description="应用统计查询参数", alias='create_time')

    class Config:
        title = "应用统计查询参数"


class StatisticSchema(Schema):
    OpenNum: int = Field(None, description="打开次数", alias="OpenNum")
    userTotalNum: int = Field(None, description="累计打开次数", alias="userTotalNum")


@router.get("/statistic/frequency", response=StatisticSchema, summary="获取统计次数")
def list_app_logs(request, filters: Filters = Query(...)) -> SuccessResponse | ErrorResponse:
    """ 获取统计次数 """
    # 获取对应数据
    result = {
        "OpenNum": len(list(retrieve(request, AppLog, [], filters).values())),
        "userTotalNum": AppLog.objects.filter(app_id=filters.app_id, status=True).count(),
    }
    # 返回结果
    return SuccessResponse(data=result)
